public class Cald_SearchDisputes {
	public Callidus_Dispute__c disputeObj {get; set;}
	public Callidus_Dispute__c firstDateObj {get; set;}
	public Callidus_Dispute__c secondDateObj {get; set;}
	
	public String filingDateOperator {get; set;}
	public String amountOperator {get; set;}
	public String status {get; set;}
	public String disputeReasonCode {get; set;}
	
	public Callidus_Dispute__c[] cdList {get; set;}
	
	public String disputePendingImageURL {get; set;}	
	
	private Callidus_Dispute_Reasons__c[] cdrList {get; set;}

	public Cald_SearchDisputes()
	{
		disputeObj = new Callidus_Dispute__c();
		firstDateObj = new Callidus_Dispute__c(Filing_Date__c = Date.today());
		secondDateObj = new Callidus_Dispute__c(Filing_Date__c = Date.today());
		
		disputePendingImageURL = '/servlet/servlet.FileDownload?file=01540000000M84Y';
		cdrList = [select Id, Name, Description__c from Callidus_Dispute_Reasons__c where status__c = 'Active'];
	}//end of constructor
	
	public List<SelectOption> getFilingDateOptions()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('%', 'Any'));
		options.add(new SelectOption('Today', 'Today'));
		options.add(new SelectOption('This Week', 'This Week'));
		options.add(new SelectOption('Last Two Weeks', 'Last Two Weeks'));
		options.add(new SelectOption('This Month', 'This Month'));
		options.add(new SelectOption('Date Range', 'Date Range'));
		return options;
	}

	public List<SelectOption> getAmountOptions()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('<', 'Less Than'));
		options.add(new SelectOption('=', 'Equals'));
		options.add(new SelectOption('>', 'Greater Than'));
		return options;
	}

	public List<SelectOption> getStatusOptions()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('%', 'Any'));
		options.add(new SelectOption('Pending Approval', 'Pending Approval'));
		options.add(new SelectOption('Withdrawn', 'Withdrawn'));
		options.add(new SelectOption('Approved', 'Approved'));
		options.add(new SelectOption('Rejected', 'Rejected'));
		return options;
	}
	
	public List<SelectOption> getDisputeReasonOptions()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('null', 'Any'));
		for(Callidus_Dispute_Reasons__c cdr : cdrList)
		{
			options.add(new SelectOption(cdr.Id, cdr.Description__c)); 
			
		}
		return options;
	}
	
	public PageReference newDispute()
	{
		Callidus_Dispute__c dObj = new Callidus_Dispute__c();
		try
		{
			dObj.Status__c = 'Draft';
			insert dObj;
			
		}
		catch(Exception e)
		{
			
		}
		return new PageReference('/apex/CCM__Cald_NewDispute?id='+dObj.id);
	}
	
	public PageReference updateDisputesObject()
	{
		try
		{
			for(Callidus_Dispute__c[] cds: [select id from Callidus_Dispute__c where Status__c = 'Draft' and CreatedBy.id = :UserInfo.getUserId()])
				delete cds;
		}
		catch(Exception e)
		{
			ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
		}
		return null;
	}
	
	public PageReference searchResults()
	{
		try
		{
			String formWhereClause = '';
            String[] statusList = new String[]{'Pending Approval', 'Approved', 'Rejected'};
			String draftStatus = 'Draft';
			String withdrawnStatus = 'Withdrawn';
			
			Double amount = disputeObj.Amount__c;
			ID myID = UserInfo.getUserId();
			
			Date tday = Date.today();
			
			Date thisWeekStart = Date.today().toStartOfWeek();
			Date thisWeekEnd = thisWeekStart.addDays(7);
			
			Date lastTwoWeeksStart = Date.today() - 14;
			Date lastTwoWeeksEnd = Date.today();

			Date thisMonthStart = Date.today().toStartOfMonth();
			Date thisMonthEnd = thisMonthStart.addMonths(1);
			
			if(status != '%' && status != 'Withdrawn')
				formWhereClause += ' Status__c = :status ';
			else if(status == 'Withdrawn')
				formWhereClause += ' Status__c = :status and OwnerId = :myID ';
			else
				formWhereClause += ' Status__c != :draftStatus and Status__c in :statusList ';
			
			if(disputeReasonCode != 'null')
				formWhereClause += ' and Dispute_Reasons__c = :disputeReasonCode ';
			
			String name =  '%'+disputeObj.Name+'%';
			if(disputeObj.Name != '')
				formWhereClause += ' and Name like :name ';
				
			if(amountOperator == '<' && amount != null)
			{
				formWhereClause += ' and Amount__c < :amount ';
			}
			else if(amountOperator == '>' && amount != null)
			{
				formWhereClause += ' and Amount__c > :amount ';
			}
			else if(amountOperator == '=' && amount != null)
			{
				formWhereClause += ' and Amount__c = :amount ';
			}
			
			if(filingDateOperator == 'Today')
				formWhereClause += ' and Filing_Date__c = :tday ';
			else if(filingDateOperator == 'This Week')
				formWhereClause += ' and Filing_Date__c >= :thisWeekStart and Filing_Date__c < :thisWeekEnd ';
			else if(filingDateOperator == 'Last Two Weeks')
				formWhereClause += ' and Filing_Date__c >= :lastTwoWeeksStart and Filing_Date__c <= :lastTwoWeeksEnd ';
			else if(filingDateOperator == 'This Month')
				formWhereClause += ' and Filing_Date__c >= :thisMonthStart and Filing_Date__c < :thisMonthEnd ';

			cdList = Database.query('select Id, Name, Status__c, Filing_Date__c, Assigned_To__c, Dispute_Reasons__r.Description__c from Callidus_Dispute__c where '+formWhereClause+' order by createddate desc limit 51');	
	//		cdList = [select Id, Name, Status__c, Filing_Date__c, Assigned_To__c, Dispute_Reasons__r.Description__c from Callidus_Dispute__c where (Status__c != 'Draft' and Status__c != 'Withdrawn' and Name like :name) or (Status__c = 'Withdrawn' and OwnerId = :UserInfo.getUserId() and Name like :name) order by createddate desc];
			if(cdList.size() == 0)
				cdList = null;
			//ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, ' - '+amount));
		}
		catch(Exception e)
		{
			ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
		}
		return null;
	}

}